import ContactAPI from '../../../../../../public/js/common/api/mybusiness/workreport/reportcontact.api.js';
const _pageInfo = {
    searchInfo: "",
    pageSize: 50,
    pageIndex: 0,
    totalPages: -1
};
Utils.NameSpace('SN.H5.WorkReportMain.ReceiveSearch');
/**
 * @file我收到的工作汇报查询
 */
(function(window, document, $) {
    let page = {..._pageInfo},checkboxListReceive=[],employeesListReceive=[],checkEmployReceive=[];
    $(function(){
        
    });

    //TODO : 唤起所有人搜索
    SN.H5.WorkReportMain.ReceiveSearch.ContactPopupClick = () => {
        $("#receive-report-contact").popup();
        $("#receive-content").empty();
        refreshQuery();
    }
    //TODO : 点击确定按钮
    SN.H5.WorkReportMain.ReceiveSearch.ContactPopupConfirmClick = () => {
       
        let subData = [...checkboxListReceive.map(item => { return [...employeesListReceive,...checkEmployReceive].find(item1 => item1.id == item); })] || [];
        checkEmployReceive = subData;
        $.closePopup();
        $('#me-search-people-text').text(`${checkEmployReceive.length == 0 ? `所有人` : `选择${checkEmployReceive.length}人`} `);
        SN.H5.WorkReportMain.setReceivePeopleList(checkboxListReceive);
        $('#receive-searchInput-cancel').trigger("click");
    }

    //TODO : 搜索人员取消
    //Date :2022-02-15 10:02:40
    //create丁平
    SN.H5.WorkReportMain.ReceiveSearch.ContactPopupCancelClick = () => {
        let checkHistory = [...checkEmployReceive.map(item => { return item.id })] || [];// 点取消时的上一次数据
        checkboxListReceive = checkHistory;// 上一次点 确定的数据  赋值给  选中的数据
        $('#receive-subEmployees').text(`确定(${checkboxListReceive.length})`);
        $("#receive-choose").text(`已选择:${checkboxListReceive.length}人`);
        $.closePopup();
        $('#receive-searchInput-cancel').trigger("click");
    }

    //TODO : 筛选 输入搜索
    //Date :2022-02-15 11:04:54
    //create丁平
    SN.H5.WorkReportMain.ReceiveSearch.InputSearch = () => {
        $('#receive-content').empty();
        refreshQuery();
    }

    //TODO : 选择筛选人事件
    //Date :2022-02-17 09:36:19
    //create丁平
    SN.H5.WorkReportMain.ReceiveSearchCheckbox = (that) => {
        let isChecked = $(that).find("input[type='checkbox']").is(':checked')
        $(that).find("input[type='checkbox']").prop("checked", !isChecked);
        if (!isChecked == true && checkboxListReceive.includes($(that).data("id")) == false) {
            checkboxListReceive.push($(that).data("id"));
        } else {
            checkboxListReceive.splice(checkboxListReceive.indexOf($(that).data("id")), 1);
        }
        $('#receive-subEmployees').text(`确定(${checkboxListReceive.length})`);
        $("#receive-choose").text(`已选择:${checkboxListReceive.length}人`);
        
    }

    const refreshQuery = () => {
        let _page = { "searchInfo": $('#receive-searchInput').val(), "pageIndex": 0, "totalPages": -1, "pageSize": 50 };
        page = Object.assign(page, _page);
        console.log($('#receive-searchInput').val());
        queryemployeesListReceive();
    }
    
    //TODO : 人员筛选上拉加载
    //Date :2022-02-17 09:00:23
    //create丁平
    SN.H5.WorkReportMain.ReceiveSearch.ScrollList = () => {
        let _data = page;
        if(_data.pageIndex == _data.totalPages)return;
              let self = this;
              if(self.loading) return;
              self.loading = true;
              setTimeout(function() {
                  self.loading = false;
                  initData();
              },500);   //模拟延迟
    }
    function unique(arr) {
        const res = new Map();
        return JSON.parse(JSON.stringify(arr)).filter((a) => !res.has(a.id) && res.set(a.id, 1))
    }
    //TODO : 查询筛选所有人
    const queryemployeesListReceive = () => {
        let _data = page;
        let _list = [];
        ContactAPI.queryEmployees(_data).then(res => {
            let _page = {
                searchInfo: $('#receive-searchInput').val(),
                pageSize: 50,
                pageIndex: _data.pageIndex + 1,
                totalPages: res.totalPages
            }
            page = Object.assign({}, _page);
            if(!Utils.IsNotNull($('#searchInput').val()) && _data.pageIndex == 0){
                _list.push(...checkEmployReceive);
            }
            _list.push(...res.items);
            unique(_list).map(item => {
                let labhtml = `<label class="weui-cell weui-cell_active weui-check__label" for="${item.id}" data-id="${item.id}" id="label-search_${item.id}">
					<div class="weui-cell__hd">
						<input type="checkbox" class="weui-check"  data-id="${item.id}" id="search-${item.id}">
						<i class="weui-icon-checked"></i>
					</div>
					<div class="weui-cell__hd" aria-hidden="true" style="position: relative;">
						<img src="../../../../public/static/images/other/pic_default_headimg.png" alt="" class="headPortrait"
							style="width: 50px;display: block;">
					</div>
					<div class="weui-cell__bd" aria-hidden="true">
						<span class="lab_name">${item.name}</span>
						<div class="demo_badge_desc lab_numCode">工号：${item.numCode}</div>
					</div>
				</label>`
                $("#receive-content").append(labhtml);
                // $(`#lab_${item.id}`).find("input[type='checkbox']").prop("checked", checkboxListReceive.includes($(this).data("id")));
            })
            checkboxListReceive.map(item => $(`#search-${item}`).prop("checked", true));
            // $("#content").empty().append(x);
            employeesListReceive = [...employeesListReceive, ...res.items];
        })
    }

})(window, document, jQuery);